package com.zdkj.subway.check.controller.subwayRat.conductorDifferences.passengersTransaction;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zdkj.common.core.util.R;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.check.entity.*;
import com.zdkj.subway.check.mapper.*;
import com.zdkj.subway.check.util.LineOrStation;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 乘客事务审核
 */
@RestController
@RequestMapping("/check")
@Slf4j
public class SwStInputPassengerWorkController {

    @Autowired
    private SwStInputPassengerWorkMapper swStInputPassengerWorkMapper;
    @Autowired
    private SwStInputPassengerWorkCashMapper swStInputPassengerWorkCashMapper;
    @Autowired
    private SwStInputPassengerWorkElectronMapper swStInputPassengerWorkElectronMapper;
    @Autowired
    private SwStInputPassengerWorkPayMapper swStInputPassengerWorkPayMapper;
    @Autowired
    private SwStInputPassengerWorkSpecialMapper swStInputPassengerWorkSpecialMapper;
    @Autowired
    private SwStInputPassengerWorkFreeMapper swStInputPassengerWorkFreeMapper;
    @Autowired
    private SwStInputPassengerWorkRefundMapper swStInputPassengerWorkRefundMapper;
    @Autowired
    private SwStInputPassengerWorkTicketMapper swStInputPassengerWorkTicketMapper;
    @Autowired
    private SwStInputPassengerWorkOtherMapper swStInputPassengerWorkOtherMapper;

    /**
     * 乘客事务审核列表
     * @param page
     * @param param
     * @return
     */
    @RequestMapping("/getSwStInputPassengerWork")
    @ApiOperation(value = "乘客事务审核列表", notes = "乘客事务审核列表")
    public R getSwStInputPassengerWork(com.baomidou.mybatisplus.extension.plugins.pagination.Page page, @RequestParam Map<String,Object> param){
        PigxUser user = SecurityUtils.getUser();
        //区分是否是线网人员
        if(user.getBelongToLine()){
            List lineList = LineOrStation.getmanagementLine();
            param.put("lines",lineList);
        }else{
            List lineList = LineOrStation.getLineOrManagementLine();
            param.put("lines",lineList);
        }
        //查询乘客事务审核列表
        IPage<SwStInputPassengerWork> swStInputPassengerWork = swStInputPassengerWorkMapper.getSwStInputPassengerWork(page,param);
        return R.ok(swStInputPassengerWork);
    }

    /**
     * 一键审批
     * @param param
     * @throws Exception
     */
    @RequestMapping("/auditAll")
    @ApiOperation(value = "一键审批", notes = "一键审批")
    public void auditAll(@RequestParam Map<String,String> param) throws Exception {
        List<Integer> ids = (List) JSONObject.parseArray(String.valueOf(param.get("ids")));
        for (Integer id : ids) {
            SwStInputPassengerWork swStInputPassengerWork = new SwStInputPassengerWork();
            swStInputPassengerWork.setId(Long.valueOf(id));
            swStInputPassengerWork.setAuditStatus("2");
            //乘客事务录入主表
            swStInputPassengerWorkMapper.updateById(swStInputPassengerWork);
            SwStInputPassengerWorkCash swStInputPassengerWorkCash = new SwStInputPassengerWorkCash();
            UpdateWrapper updateWrapper1 = new UpdateWrapper();
            updateWrapper1.eq("RELATION_ID",id);
            swStInputPassengerWorkCash.setAuditStatus("2");
            //乘客事务录入明细表【现金事务】
            swStInputPassengerWorkCashMapper.update(swStInputPassengerWorkCash,updateWrapper1);
            SwStInputPassengerWorkElectron swStInputPassengerWorkElectron = new SwStInputPassengerWorkElectron();
            swStInputPassengerWorkElectron.setAuditStatus("2");
            //乘客事务录入明细表【电子事务】
            swStInputPassengerWorkElectronMapper.update(swStInputPassengerWorkElectron,updateWrapper1);
            SwStInputPassengerWorkPay swStInputPassengerWorkPay = new SwStInputPassengerWorkPay();
            swStInputPassengerWorkPay.setAuditStatus("2");
            //乘客事务录入明细表【付费出站票发售】
            swStInputPassengerWorkPayMapper.update(swStInputPassengerWorkPay,updateWrapper1);
            SwStInputPassengerWorkSpecial swStInputPassengerWorkSpecial = new SwStInputPassengerWorkSpecial();
            swStInputPassengerWorkSpecial.setAuditStatus("2");
            //乘客事务录入明细表【特殊车票发售】
            swStInputPassengerWorkSpecialMapper.update(swStInputPassengerWorkSpecial,updateWrapper1);
            SwStInputPassengerWorkFree swStInputPassengerWorkFree = new SwStInputPassengerWorkFree();
            swStInputPassengerWorkFree.setAuditStatus("2");
            //乘客事务录入明细表【免费出站票发售】
            swStInputPassengerWorkFreeMapper.update(swStInputPassengerWorkFree,updateWrapper1);
            SwStInputPassengerWorkRefund swStInputPassengerWorkRefund = new SwStInputPassengerWorkRefund();
            swStInputPassengerWorkRefund.setAuditStatus("2");
            //乘客事务录入明细表【故障退款】
            swStInputPassengerWorkRefundMapper.update(swStInputPassengerWorkRefund,updateWrapper1);
            SwStInputPassengerWork swStInputPassengerWork1 = swStInputPassengerWorkMapper.selectById(id);
            SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd");
            Date workDate = swStInputPassengerWork1.getWorkDate();
            String workDateStr = dff.format(workDate);
            Map req = new HashMap();
            req.put("auditStatus","2");
            req.put("handlePersonCode",swStInputPassengerWork1.getConductorCode());
            req.put("handleTime",workDateStr);
            req.put("lineCode",swStInputPassengerWork1.getLineCode());
            req.put("stationCode",swStInputPassengerWork1.getStationCode());
            //乘客事务退换票
            swStInputPassengerWorkTicketMapper.updateSwStInputPassengerWorkTicket(req);
            req.put("conductorCode",swStInputPassengerWork1.getConductorCode());
            req.put("inputTime",workDateStr);
            //其他相关数据录入
            swStInputPassengerWorkOtherMapper.updateSwStInputPassengerWorkOther(req);

        }
    }

}
